package com.zensun.common.core.domain;


import com.zensun.common.constant.HttpStatus;
import lombok.Data;


/**
 * 操作消息提醒
 *
 * @author gmk
 */
@Data
public class R<T> {
    private int code;
    private String msg;
    private T data;
    private Object obj;

    private R(int code, String msg, T data, Object obj) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.obj = obj;
    }


    public static <T> R<T> success() {
        return R.success("操作成功");
    }

    public static <T> R<T> success(T data) {
        return R.success("操作成功", data);
    }

    public static <T> R<T> success(String msg) {
        return R.success(msg, null);
    }

    public static <T> R<T> success(String msg, T data) {
        return R.success(msg, data, null);
    }

    public static <T> R<T> success(T data, Object obj) {
        return R.success("操作成功", data, obj);
    }

    public static <T> R<T> success(String msg, T data, Object obj) {
        return new R<T>(HttpStatus.SUCCESS, msg, data, obj);
    }

    public static <T> R<T> error() {
        return R.error("操作失败");
    }

    public static <T> R<T> error(T data) {
        return R.error("操作失败", data);
    }

    public static <T> R<T> error(String msg) {
        return R.error(msg, null);
    }

    public static <T> R<T> error(int code, String msg) {
        return new R<T>(code, msg, null, null);
    }

    public static <T> R<T> error(String msg, T data) {
        return new R<T>(HttpStatus.ERROR, msg, data, null);
    }

    public static R toAjax(int i) {
        return i > 0 ? R.success() : R.error();
    }

    public static R toAjax(boolean i) {
        return i ? R.success() : R.error();
    }
}
